package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.PipeHealthTjController;
import com.antoco.operation.bigscreen.model.dto.PipeHealthTjDTO;
import com.antoco.operation.bigscreen.model.entity.PipeHealthTj;
import com.antoco.operation.bigscreen.mapper.PipeHealthTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.PipeHealthTjService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/pipe-health-tj")
@RestController
public class PipeHealthTjControllerImpl implements PipeHealthTjController {

    @Autowired
    private PipeHealthTjService pipeHealthTjService;

    @Autowired
    private PipeHealthTjMapper pipeHealthTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public PipeHealthTjDTO save(@Validated @RequestBody PipeHealthTjDTO pipeHealthTjDTO) {
        PipeHealthTj pipeHealthTj = pipeHealthTjMapper.asEntity(pipeHealthTjDTO);
        pipeHealthTjService.save(pipeHealthTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeHealthTjDTO;
    }

    @Override
    @GetMapping("/{id}")
    public PipeHealthTjDTO findById(@PathVariable("id") String id) {
        PipeHealthTj pipeHealthTj = pipeHealthTjService.findById(id).orElse(null);
        return pipeHealthTjMapper.asDTO(pipeHealthTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        pipeHealthTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<PipeHealthTjDTO> list() {
        return pipeHealthTjMapper.asDTOList(pipeHealthTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<PipeHealthTjDTO> pageQuery(Pageable pageable) {
        Page<PipeHealthTj> pipeHealthTjPage = pipeHealthTjService.findAll(pageable);
        List<PipeHealthTjDTO> dtoList = pipeHealthTjPage
                .stream()
                .map(pipeHealthTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, pipeHealthTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public PipeHealthTjDTO update(@RequestBody PipeHealthTjDTO pipeHealthTjDTO, @PathVariable("id") String id) {
        PipeHealthTj pipeHealthTj = pipeHealthTjMapper.asEntity(pipeHealthTjDTO);
        pipeHealthTjMapper.asDTO(pipeHealthTjService.update(pipeHealthTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeHealthTjDTO;
    }
}